<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Dialogs_mod extends CI_Model {

    private $_dbTableMessages = 'messages';
    private $_dbTableUsers = 'users';
    private $_dbTableImages = 'images';
    function __construct() {
        // Call the Model constructor
        parent::__construct();
    }

    public function getFromUsers() {
      
        $query = $this->db->select(
                        $this->_dbTableUsers . '.id,' .
                        $this->_dbTableUsers . '.username'
                )
                ->from($this->_dbTableUsers)
                ->join($this->_dbTableMessages, $this->_dbTableUsers . ".id = " . $this->_dbTableMessages . ".from")
                ->distinct()
                ->get();
        
         //echo $this->db->last_query();
        $errors = $this->db->_error_message();
        if (!empty($errors)) {
          throw new Exception("SQL Error: " . $errors);
        } else { }

        if ($query->num_rows() > 0) {
          $result = $query->result();
          $query->free_result();
        } else {
          $result = NULL;
        }        
        
        return $result;
    }

    public function getToUsers($from) {
       
       if( $from == "" ) return NULL;
       
       $this->db->select(
                        $this->_dbTableUsers . '.id,' .
                        $this->_dbTableUsers . '.username'
                )
                ->from($this->_dbTableUsers)
                ->join($this->_dbTableMessages, $this->_dbTableUsers . ".id =".$this->_dbTableMessages . ".to")
                ->where($this->_dbTableMessages . ".from = " . $from)
                ->distinct();
        $query = $this->db->get();
        
        //echo $this->db->last_query();
        $errors = $this->db->_error_message();
        if (!empty($errors)) {
          throw new Exception("SQL Error: " . $errors);
        } else { }

        if ($query->num_rows() > 0) {
          $result = $query->result();
          $query->free_result();
        } else {
          $result = NULL;
        }
        
        $empty = array();
        $empty[0]->id = "";
        $empty[0]->username = "";
        
        $result = array_merge($empty,$result);
       
        return $result;
    }

 
    public function getDialog($filter = NULL) {
                
        $where = "";
        
        if( isset($filter['from']) && $filter['from'] != "" )
        {
          $where .= "( ";
          $where .=  $this->_dbTableMessages . ".from = " . $filter['from'];
          $where .= " OR " . $this->_dbTableMessages . ".to = " . $filter['from'];
          $where .= " )";
        }
        if( isset($filter['to']) && $filter['to'] != "" )
        {
          if( isset($filter['from']) && $filter['from'] != "" ){
              $where .= " AND ";
          }    
          $where .= "( " . $this->_dbTableMessages . ".to = " . $filter['to']; 
          $where .= " OR ";        
          $where .=  $this->_dbTableMessages . ".from = " . $filter['to']; 
          $where .= " ) ";
        }
        
        $this->db->select
                ("time, content, from, to, "
                        . " (SELECT username FROM {$this->_dbTableUsers} WHERE {$this->_dbTableUsers}.`id` = `from`) as username_from, "
                       
                        . " (SELECT username FROM {$this->_dbTableUsers} WHERE {$this->_dbTableUsers}.`id` = `to` ) as username_to, "
                        
                        . "( SELECT name  FROM {$this->_dbTableImages} WHERE `private`=0 and {$this->_dbTableImages}.`user_id`= `from` ORDER BY main desc LIMIT 1) as image_name_from, "
                        
                        . "( SELECT name  FROM {$this->_dbTableImages} WHERE `private`=0 and {$this->_dbTableImages}.`user_id`= `to` ORDER BY main desc LIMIT 1) as image_name_to "
                      );
                $this->db->from($this->_dbTableMessages);
                
                if($where != "")
                  $this->db->where($where);
                           
                $this->db->order_by($this->_dbTableMessages.'.id', 'desc');
        
        $query = $this->db->get();
        
        //echo $this->db->last_query();
        $errors = $this->db->_error_message();
        if (!empty($errors)) {
          throw new Exception("SQL Error: " . $errors);
        } else { }

        if ($query->num_rows() > 0) {
          $messages = $query->result();
          $query->free_result();
        } else {
          $messages = NULL;
        }
       
        return $messages;
    }

}

?>
